package com.dqp.cslggroup.Util;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.preference.PreferenceManager;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;

public class CompressedBitmap {



    public static Bitmap Compressed(Bitmap bitmap, float x){
        Matrix matrix = new Matrix();
        matrix.setScale(x, x);
        bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        return bitmap;
    }

    public static Boolean fileExist(String path){
        if (path!=null) {
            File f = new File(path);
            return f.exists() && f.isFile();
        }
        return false;

    }

    public static void reSeting(Context context,String path){
        SharedPreferences sp;
        SharedPreferences.Editor editor;
        sp = PreferenceManager.getDefaultSharedPreferences(context);
        editor = sp.edit();
        editor.remove(path);
        editor.apply();
    }

    public static long getFileSize(File file) throws Exception {
        if (file == null) {
            return 0;
        }
        long size = 0;
        if (file.exists()) {
            FileInputStream fis = new FileInputStream(file);
            size = fis.available();
        }
        return size;
    }

    public static byte[] picTobyte(String path){
        byte[] fileByte = new byte[0];
        File file = new File(path);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            try {
                fileByte = Files.readAllBytes(file.toPath());
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return  fileByte;
    }


}
